#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e3+5,mod=1e9+7;
int n,m,de[N],a[N],len,u,v,ans,dp[N][N],f[N],dp1[N],g[N];
vector <int> e[N],g1[N];
void dfs(int u,int fa){
	for(int i = 0;i < g1[u].size();i++){
		int v= g1[u][i];
		if(v==fa)continue;
		de[v]=de[u]+1,dfs(v,u);f[v]=u;
	}
}int b[N],vis[N],us[N];
void calc(int x,int y){
	vis[x]=vis[y]=1;
	if(x==y)return;
	if(de[x]<de[y])swap(x,y);
	while(de[x]>de[y])x=f[x],vis[x]=1;
	if(x==y)return;
	while(x!=y){
		vis[x]=vis[y]=1;
		x=f[x],y=f[y];
	}vis[x]=1;
}
int gettlh(){
	for(int i = 1;i <= m;i++)us[i]=0;
	for(int i = 1;i <= n;i++)vis[i]=0;
	for(int i = 1;i <= len;i++){
		for(int j = i;j <= len;j++){
				
			 calc(b[i],b[j]);
		}
	}
	for(int i = 1;i <= n;i++){
		if(vis[i]){
			for(int j = 0;j < e[i].size();j++)us[e[i][j]]=1;
		}
	}	
	int cnt=0;
	for(int i = 1;i <= m;i++)if(us[i])cnt++;
	return cnt;
}
signed main(){
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i++)cin >> a[i];
	for(int i = 1;i <= n;i++){
		cin >> len;
		for(int j = 1,x;j <= len;j++)cin >> x,e[i].push_back(x);
	}
	for(int i = 1;i < n;i++){
		cin >> u >>v;
		g1[u].push_back(v);
		g1[v].push_back(u);
	}dfs(1,0);
	if(m==1){
		dp[0][0]=1;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= i;j++){
				dp[i][j]=dp[i-1][j-1];
				dp[i][j]=(dp[i][j]+dp[i-1][j]*j)%mod;
			}
		}
		for(int i = 1;i <= n;i++)ans=(ans+a[1]*i%mod*dp[n][i]%mod)%mod;
		cout << ans;
	}
	return 0;
}

